package com.hcb.micro.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Discuss;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.QqbsAccount;
import com.hcb.micro.service.IDiscussService;
import com.hcb.micro.service.IQqbsAccountService;


import net.sf.json.JSONObject;

@Controller
@RequestMapping("discuss")
public class DiscussController extends BaseControllers{

	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IDiscussService discussService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String listByType() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			para.put("accountId", bodyInfo.getString("accountId"));
		}
		
		if(bodyInfo.get("productSellerId") != null && !"".equals(bodyInfo.get("productSellerId"))) {
			para.put("productSellerId", bodyInfo.getString("productSellerId"));
		}
		
		
		List<Discuss> dListAll = discussService.selectByPara(para);
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Discuss> dList = discussService.selectByPara(para);
		if(dList.size() <= 0){
			json.put("result", "1");
			json.put("description", "评论列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,Object>> list = new ArrayList<>();
		for(Discuss d:dList){
			String nickname = "";
			String image = "";
			QqbsAccount qqbsAccount = qqbsAccountService.selectByAccountId(d.getAccountId());
			if(qqbsAccount != null){
				nickname = qqbsAccount.getNickname()==null?"":qqbsAccount.getNickname();
				image = qqbsAccount.getImage()==null?"":qqbsAccount.getImage();
			}
			Map<String,Object> map = new HashMap<>();
			map.put("id", d.getId());
			map.put("accountId", d.getAccountId());
			map.put("nickname", nickname);
			map.put("image", image);
			map.put("unionId", d.getUnionId());
			map.put("status", d.getStatus());
			
			//评价状态：1 申请中   2  展现  3   不展现
			String statusValue = "";
			switch(d.getStatus()){
			case 1:
				statusValue = "申请中";
				break;
			case 2:
				statusValue = "展现";
				break;
			case 3:
				statusValue = "不展现";
				break;
			}
			
			map.put("statusValue", statusValue);
			map.put("content", d.getContent());
			
			/*List<String> imageList = new ArrayList<>();
			if(d.getImage1() != null && !"".equals(d.getImage1())){
				imageList.add(d.getImage1());
			}
			if(d.getImage2() != null && !"".equals(d.getImage2())){
				imageList.add(d.getImage2());
			}
			if(d.getImage3() != null && !"".equals(d.getImage3())){
				imageList.add(d.getImage3());
			}
			if(d.getImage4() != null && !"".equals(d.getImage4())){
				imageList.add(d.getImage4());
			}
			if(d.getImage5() != null && !"".equals(d.getImage5())){
				imageList.add(d.getImage5());
			}*/
			
			map.put("createTime", sdf.format(d.getCreateTime()));
			map.put("updateTime", sdf.format(d.getUpdateTime()));
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取评价列表成功");
		json.put("list", list);
		json.put("count", dListAll.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		int right = 1;
		if(bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Discuss d = discussService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("id")));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		if(d == null){
			json.put("result", "1");
			json.put("description", "评价不存在");
			return buildReqJsonObject(json);
		}
		
		Map<String,Object> map = new HashMap<>();
			
		QqbsAccount qqbsAccount = qqbsAccountService.selectByAccountId(d.getAccountId());
		
		
		map.put("id", d.getId());
		map.put("accountId", d.getAccountId());
		map.put("unionId", d.getUnionId());
		map.put("status", d.getStatus());
		
		//评价状态：1 申请中   2  展现  3   不展现
		String statusValue = "";
		switch(d.getStatus()){
		case 1:
			statusValue = "申请中";
			break;
		case 2:
			statusValue = "展现";
			break;
		case 3:
			statusValue = "不展现";
			break;
		}
		
		map.put("statusValue", statusValue);
		map.put("content", d.getContent());
		
		if(d.getImage1() != null && !"".equals(d.getImage1())){
			map.put("image1", d.getImage1());
		}else{
			map.put("image1", "");
		}
		
		if(d.getImage2() != null && !"".equals(d.getImage2())){
			map.put("image2", d.getImage2());
		}else{
			map.put("image2", "");
		}
		
		if(d.getImage3() != null && !"".equals(d.getImage3())){
			map.put("image3", d.getImage3());
		}else{
			map.put("image3", "");
		}
		
		if(d.getImage4() != null && !"".equals(d.getImage4())){
			map.put("image4", d.getImage4());
		}else{
			map.put("image4", "");
		}
		
		if(d.getImage5() != null && !"".equals(d.getImage5())){
			map.put("image5", d.getImage5());
		}else{
			map.put("image5", "");
		}
		
		map.put("nickname", qqbsAccount.getNickname()==null?"":qqbsAccount.getNickname());
		map.put("image", qqbsAccount.getImage()==null?"":qqbsAccount.getImage());
		map.put("createTime", sdf.format(d.getCreateTime()));
		map.put("updateTime", sdf.format(d.getUpdateTime()));
		
    	json.put("result", "0");
		json.put("description", "获取评价详情成功");
		json.put("discuss", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Date date = new Date();
		
		Discuss d = new Discuss();
		
		if(bodyInfo.get("unionId") != null && !"".equals(bodyInfo.get("unionId"))) {
			d.setUnionId(bodyInfo.getInt("unionId"));
		}
		
		if(bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			d.setAccountId(bodyInfo.getInt("accountId"));
		}
		
		if(bodyInfo.get("content") != null && !"".equals(bodyInfo.get("content"))) {
			d.setContent(bodyInfo.getString("content"));
		}
		
		if(bodyInfo.get("status") != null && !"".equals(bodyInfo.get("status"))) {
			d.setStatus(bodyInfo.getInt("status"));
		}
		
		if(bodyInfo.get("image1") != null && !"".equals(bodyInfo.get("image1"))) {
			d.setImage1(bodyInfo.getString("image1"));
		}
		if(bodyInfo.get("image2") != null && !"".equals(bodyInfo.get("image2"))) {
			d.setImage2(bodyInfo.getString("image2"));
		}
		if(bodyInfo.get("image3") != null && !"".equals(bodyInfo.get("image3"))) {
			d.setImage3(bodyInfo.getString("image3"));
		}
		if(bodyInfo.get("image4") != null && !"".equals(bodyInfo.get("image4"))) {
			d.setImage4(bodyInfo.getString("image4"));
		}
		if(bodyInfo.get("image5") != null && !"".equals(bodyInfo.get("image5"))) {
			d.setImage5(bodyInfo.getString("image5"));
		}
		
		d.setCreateTime(date);
		d.setUpdateTime(date);
		d.setStarCount(5);
		
		
		Integer insert  = discussService.insertSelective(d);
		if(insert == 0){
			json.put("result", "1");
			json.put("description", "创建评价失败");
			return buildReqJsonObject(json);
		}
		
		json.put("result", "0");
		json.put("description", "创建评价成功");
		json.put("discussId", d.getId());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Date date = new Date();
		
		Discuss d = discussService.selectByPrimaryKey(bodyInfo.getInt("id"));
		
		if(d == null){
			json.put("result", "1");
			json.put("description", "评价不存在");
			return buildReqJsonObject(json);
		}
		
		if(bodyInfo.get("unionId") != null && !"".equals(bodyInfo.get("unionId"))) {
			d.setUnionId(bodyInfo.getInt("unionId"));
		}
		
		if(bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			d.setAccountId(bodyInfo.getInt("accountId"));
		}
		
		if(bodyInfo.get("content") != null && !"".equals(bodyInfo.get("content"))) {
			d.setContent(bodyInfo.getString("content"));
		}
		
		if(bodyInfo.get("status") != null && !"".equals(bodyInfo.get("status"))) {
			d.setStatus(bodyInfo.getInt("status"));
		}
		
		if(bodyInfo.get("image1") != null && !"".equals(bodyInfo.get("image1"))) {
			d.setImage1(bodyInfo.getString("image1"));
		}
		if(bodyInfo.get("image2") != null && !"".equals(bodyInfo.get("image2"))) {
			d.setImage2(bodyInfo.getString("image2"));
		}
		if(bodyInfo.get("image3") != null && !"".equals(bodyInfo.get("image3"))) {
			d.setImage3(bodyInfo.getString("image3"));
		}
		if(bodyInfo.get("image4") != null && !"".equals(bodyInfo.get("image4"))) {
			d.setImage4(bodyInfo.getString("image4"));
		}
		if(bodyInfo.get("image5") != null && !"".equals(bodyInfo.get("image5"))) {
			d.setImage5(bodyInfo.getString("image5"));
		}
		
		d.setUpdateTime(date);
		
		Integer update  = discussService.updateByPrimaryKeySelective(d);
		if(update == 0){
			json.put("result", "1");
			json.put("description", "修改评价失败");
			return buildReqJsonObject(json);
		}
		
		json.put("result", "0");
		json.put("description", "修改评价成功");
		json.put("discussId", d.getId());
		return buildReqJsonObject(json);
	}
}
